package com.cwz.security.interceptor;

import com.cwz.api.model.LoginUser;
import com.cwz.core.constant.SecurityConstants;
import com.cwz.core.context.SecurityContextHolder;
import com.cwz.core.utils.servle.ServletUtils;
import com.cwz.core.utils.string.StringUtils;
import com.cwz.security.auth.AuthUtil;
import com.cwz.security.utils.SecurityUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.AsyncHandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: w-demo
 * @description: 自定义请求头拦截器，将 Header 数据封装到线程变量中方便获取
 * 注意：此拦截器会同时验证当前用户有效期自动刷新有效期
 * @author: Wen
 **/
public class HeaderInterceptor implements AsyncHandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		if (!(handler instanceof HandlerMethod)) return true;
		SecurityContextHolder.setUserId(ServletUtils.getHeader(request, SecurityConstants.DETAILS_USER_ID));
		SecurityContextHolder.setUserName(ServletUtils.getHeader(request, SecurityConstants.DETAILS_USERNAME));
		SecurityContextHolder.setUserKey(ServletUtils.getHeader(request, SecurityConstants.USER_KEY));
		SecurityContextHolder.setCompany(ServletUtils.getHeader(request, SecurityConstants.DETAILS_COMPANY));

		String token = SecurityUtils.getToken();
		if (StringUtils.isNotEmpty(token)) {
			LoginUser loginUser = AuthUtil.getLoginUser(token);
			if (StringUtils.isNotNull(loginUser)) {
				AuthUtil.verifyLoginUserExpire(loginUser);
				SecurityContextHolder.set(SecurityConstants.LOGIN_USER, loginUser);
			}
		}
		return true;
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		SecurityContextHolder.remove();
	}
}
